﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Stauffware.Common.Core
{
	/// <summary>
	/// Extension methods for the System.String class.
	/// </summary>
	public static class String_Extensions
	{
		//-----------------------------------------------------------------------------
		/// <summary>
		/// Extracts a substring from this string and returns it.
		/// </summary>
		/// <param name="input">the string to extract from</param>
		/// <param name="start">the index of the substring to be extracted</param>
		/// <param name="length">the length of the substring to be extracted</param>
		/// <param name="output">a copy of this string, without the extracted substring</param>
		/// <returns>the extracted substring</returns>
		
		public static string Extract (this string input, int start, int length, 
																out string output)
		{
			string retStr = input.Substring(start, length);
			output = input.Remove(start, length);
			return retStr;
		}

		//-----------------------------------------------------------------------------
		/// <summary>
		/// Returns whether the a string contains a character.
		/// </summary>
		/// <param name="input">the string to search in</param>
		/// <param name="c">the character to search for</param>
		/// <returns>true if c was found in input, otherwise false</returns>
		
		public static bool Contains (this string input, char c)
		{
			return input.IndexOf(c) >= 0;
		}

		//-----------------------------------------------------------------------------
		/// <summary>
		/// Looks for one string in another, and returns the offset of the first
		/// character after the found string.  Equivalent to:
		///		searchIn.IndexOf(searchFor) + searchFor.Length
		/// </summary>
		/// <param name="searchIn">the string to search in</param>
		/// <param name="searchFor">the string to search for</param>
		/// <returns>
		///		The offset in searchIn of the first instance of searchFor, plus
		///		the length of searchFor.  Returns -1 if not found.
		/// </returns>
		
		public static int IndexOfEnd (this string searchIn, string searchFor)
		{
			int n = searchIn.IndexOf(searchFor);
			if (n < 0)
				return n;
			else return n + searchFor.Length;
		}

		//-----------------------------------------------------------------------------
		/// <summary>
		/// Trims the given number of characters from the end of the string.
		/// </summary>
		/// <param name="input">the string to trim</param>
		/// <param name="charsToTrim">
		///		the number of characters to trim; 
		///		must not be greater than the length of the string
		/// </param>
		/// <returns>the trimmed string</returns>

		public static string TrimEnd (this string input, int charsToTrim)
		{
			return input.Substring(0, input.Length - charsToTrim);
		}

		//-----------------------------------------------------------------------------
	}
}
